/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:xml/xml.dart';

class Construct extends StatefulWidget {
  const Construct({super.key});

  @override
  State<Construct> createState() => _ConstructState();
}

class _ConstructState extends State<Construct> {
  String constructXml() {
    final builder = XmlBuilder();
    builder.processing('xml', 'version="1.0"');
    builder.element('note', nest: () {
      builder.element('to', nest: 'Tove');
      builder.element('from', nest: 'Jani');
      builder.element('heading', nest: 'Reminder');
      builder.element('body', nest: "Don't forget me this weekend!");
    });
    final bookXml = builder.buildDocument();

    return (bookXml.toXmlString(pretty: true));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('XML 构建'),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 10),
            Text("XML构建过程："),
            SizedBox(height: 10),
            Container(
              margin: EdgeInsets.all(7),
              color: Color.fromARGB(255, 236, 236, 236),
              child: Text('''  final builder = XmlBuilder();
    builder.processing('xml', 'version="1.0"');
    builder.element('note', nest: () {
      builder.element('to', nest: 'Tove');
      builder.element('from', nest: 'Jani');
      builder.element('heading', nest: 'Reminder');
      builder.element('body', nest: "Don't forget me this weekend!");
    });
    final bookshelfXml = builder.buildDocument();'''),
            ),
            SizedBox(height: 10),
            Text("XML构建效果："),
            SizedBox(height: 10),
            Container(margin: EdgeInsets.all(7), color: Color.fromARGB(255, 236, 236, 236), child: Text(constructXml())),
          ],
        ),
      ),
    );
  }
}
